package logic;
/*
 * This class is part of the book "iText in Action - 2nd Edition"
 * written by Bruno Lowagie (ISBN: 9781935182610)
 * For more info, go to: http://itextpdf.com/examples/
 * This example only works with the AGPL version of iText.
 */


import gui.AcademicFrame;
import gui.Dialog;
import java.awt.Desktop;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Font;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;
import common.ChatIF;


public class PDFGenerator implements ChatIF{

    /** Path to the resulting PDF file. */
    public static final String RESULT = "C:/Users/hp/Desktop/hello.pdf";
    
    /**
     * Creates a PDF file: hello.pdf
     * @param    args    no arguments needed
     */
    public static void main(String[] args)
    	throws DocumentException, IOException {}
    
    /**
     * Creates a PDF document.
     * @param filename the path to the new PDF document
     * @throws    DocumentException 
     * @throws    IOException 
     */
    public void test1(){
    	System.out.println("in test1");
    }
    public static void test2(){
    	for(int i=0;i<100;i++)
    	System.out.println("in test2");
    }  
    public static void createActivityReport(String filename ,String from , String to , 
    		String num1 , String num2, String num3, String num4, String num5 , 
    		double[] colData1, double[] colData2, double[] colData3, double[] colData4, double[] colData5)
    {
    	byte bytes[];
    	double med1, med2, med3, med4, med5;
    	double sd1, sd2, sd3, sd4, sd5;
    	double fd1, fd2, fd3, fd4, fd5;
    	ArrayList<Object> message=new ArrayList<Object>();
    	try{
    			
    	System.out.println("in createActivityReport");

    	med1=median(colData1);
    	med2=median(colData2);
    	med3=median(colData3);
    	med4=median(colData4);
    	med5=median(colData5);
    	sd1=standardDeviation(colData1);
    	sd2=standardDeviation(colData2);
    	sd3=standardDeviation(colData3);
    	sd4=standardDeviation(colData4);
    	sd5=standardDeviation(colData5);
    	fd1=frequencyDistribution(colData1,num1);
    	fd2=frequencyDistribution(colData2,num2);
    	fd3=frequencyDistribution(colData3,num3);
    	fd4=frequencyDistribution(colData4,num4);
    	fd5=frequencyDistribution(colData5,num5);
        Document document = new Document();
      
        Font font1 = new Font(Font.FontFamily.HELVETICA, 27, Font.BOLD);
        Font font2 = new Font(Font.FontFamily.HELVETICA, 20);
 
        Number fileNumber = Math.random() * 1000;
		String fileName = fileNumber.toString() + ".pdf";
		File file = new File(fileName);
		PdfWriter.getInstance(document, new FileOutputStream(file));
        document.open();
        document.add(new Paragraph("Activity report for "+from,font1));
        document.add(new Paragraph(" ",font1));
        
        document.add(new Paragraph("Number of active requests: "+num1,font2));
        document.add(new Paragraph("Median for active requests: "+med1,font2));
        document.add(new Paragraph("Standard deviation of active requests: "+sd1,font2));
        document.add(new Paragraph("Frequency distribution of active requests: "+fd1,font2));
        
        document.add(new Paragraph(" ",font1));
        
        document.add(new Paragraph("Number of frozen requests: "+num2,font2));
        document.add(new Paragraph("Median of frozen requests: "+med2,font2));
        document.add(new Paragraph("Standard deviation of frozen requests: "+sd2,font2));
        document.add(new Paragraph("Frequency distribution of frozen requests: "+fd2,font2));
        document.add(new Paragraph(" ",font1));
        
        document.add(new Paragraph("Number of closed requests: "+num3,font2));
        document.add(new Paragraph("Median of closed requests: "+med3,font2));
        document.add(new Paragraph("Standard deviation of closed requests: "+sd3,font2));
        document.add(new Paragraph("Frequency distribution of closed requests: "+fd3,font2));
        document.add(new Paragraph(" ",font1));
        
        document.add(new Paragraph("Number of denied requests: "+num4,font2));
        document.add(new Paragraph("Median of denied requests: "+med4,font2));
        document.add(new Paragraph("Standard deviation of denied requests: "+sd4,font2));
        document.add(new Paragraph("Frequency distribution of denied requests: "+fd4,font2));
        document.add(new Paragraph(" ",font1));
        
        document.add(new Paragraph("Number of total work days: "+num5,font2));
        document.add(new Paragraph("Median of total work days: "+med5,font2));  
        document.add(new Paragraph("Standard deviation of total work days: "+sd5,font2));
        document.add(new Paragraph("Frequency distribution of total work days: "+fd5,font2));
        document.add(new Paragraph(" ",font1));
        
        
        document.add(new Paragraph("Team 8",font2));
        document.close();  

		Desktop.getDesktop().open(file); 
		
		int len = (int) file.length();
		FileInputStream fis = new FileInputStream(file);
		bytes = new byte[len];
		fis.read(bytes, 0, len);
		int fileLength=len;
			
        message.add("setActivityReport");
        message.add(from);
        message.add(bytes);
        message.add(fileLength);
        System.out.println("after message.add");
        AcademicFrame.myclient.sendMessageToServer(message);     
    	}    	
    	catch(DocumentException e){
    		System.out.println("error");
    	}
    	catch (IOException e) {
    		System.out.println("error");
		}
	}
    public static void createExecutionReport(String filename ,String from , String to , String num1 , String num2) 	throws DocumentException, IOException
    {
        // step 1
        Document document = new Document();

        
        Font font1 = new Font(Font.FontFamily.HELVETICA, 27, Font.BOLD);
        Font font2 = new Font(Font.FontFamily.HELVETICA, 20);
        // step 2
        Number fileNumber = Math.random() * 1000;
		String fileName = fileNumber.toString() + ".pdf";
        PdfWriter.getInstance(document, new FileOutputStream(fileName));
        // step 3

        document.open();
        // step 4
        document.add(new Paragraph("Execution report from "+from+" to "+to,font1));
        document.add(new Paragraph(" ",font1));
        document.add(new Paragraph("Number of extentions made: "+num1,font2));
        document.add(new Paragraph("Number of days added to initial estimated duration: "+num2,font2));
 
        document.add(new Paragraph(" ",font1));
        document.add(new Paragraph("Team 8",font2));

        // step 5
        document.close();
        
        Desktop.getDesktop().open(new File(fileName)); 
	}
    public static void createDelaysReport(String filename ,String from , String to , String num1 , String num2, String num3, String num4, String num5, String num6, 
    		String num7, String num8, String num9, String num10, String num11, String num12) 	throws DocumentException, IOException
    {
        // step 1
        Document document = new Document();

        
        Font font1 = new Font(Font.FontFamily.HELVETICA, 27, Font.BOLD);
        Font font2 = new Font(Font.FontFamily.HELVETICA, 20);
        // step 2
        Number fileNumber = Math.random() * 1000;
		String fileName = fileNumber.toString() + ".pdf";
        PdfWriter.getInstance(document, new FileOutputStream(fileName));
        // step 3

        document.open();
        // step 4
        document.add(new Paragraph("Delays report from "+from+" to "+to,font1));
        document.add(new Paragraph(" ",font1));

        int numOfDelays= Integer.parseInt(num1)+Integer.parseInt(num2)+Integer.parseInt(num3)+Integer.parseInt(num4)+Integer.parseInt(num5)+Integer.parseInt(num6);
        int numOfDelayedDays= Integer.parseInt(num7)+Integer.parseInt(num8)+Integer.parseInt(num9)+Integer.parseInt(num10)+Integer.parseInt(num11)+Integer.parseInt(num12);
        
        document.add(new Paragraph("Number of delays: "+numOfDelays,font2));
        document.add(new Paragraph("Number of delayed days: "+numOfDelayedDays,font2));
        
        document.add(new Paragraph("Number of delays for 'Information Station' system: "+num1,font2));
        document.add(new Paragraph("Number of delays for 'Moodle' system: "+num2,font2));
        document.add(new Paragraph("Number of delays for 'Library' system: "+num3,font2));
        document.add(new Paragraph("Number of delays for 'Class Computers' system: "+num4,font2));
        document.add(new Paragraph("Number of delays for 'Computer Farm' system: "+num5,font2));
        document.add(new Paragraph("Number of delays for 'Braude Website' system: "+num6,font2));
        
        document.add(new Paragraph("Number of delayed days for 'Information Station' system: "+num7,font2));
        document.add(new Paragraph("Number of delayed days for 'Moodle' system: "+num8,font2));
        document.add(new Paragraph("Number of delayed days for 'Library' system: "+num9,font2));
        document.add(new Paragraph("Number of delayed days for 'Class Computers' system: "+num10,font2));
        document.add(new Paragraph("Number of delayed days for 'Computer Farm' system: "+num11,font2));
        document.add(new Paragraph("Number of delayed days for 'Braude Website' system: "+num12,font2));
        
        document.add(new Paragraph(" ",font1));
        document.add(new Paragraph("Team 8:",font2));

        // step 5
        document.close();
        
        Desktop.getDesktop().open(new File(fileName)); 
	}
    public static double median(double[] arr){
    	double result=0;
    	Arrays.sort(arr);
    	int length=arr.length;
    	if(length==1){
    		result=arr[0];
    	}
    	else if(length%2==0){
    		result=(arr[length/2]+arr[length/2-1])/2;    		
    	}
    	else{
    		result=arr[length/2]; 
    	}    	
    	return result;
    }
    public static double standardDeviation(double[] arr){
    	double avg=0;
    	double sum=0;
    	double temp=0;
    	Arrays.sort(arr);
    	int length=arr.length;
    	if(length==1){
    		sum=0;
    	}
    	else{
    		for(int i=0; i<length; i++){
    			avg+= arr[i];
    		}
    		avg=avg/length;

    		for(int i=0; i<length; i++){
    			temp=(arr[i]-avg);
    			temp=Math.pow(temp, 2);
    			sum+=temp;
    		}
    		sum=sum/length;
    		sum=Math.sqrt(sum);
    	}    	
    	return sum;
    }
//frequencyDistribution
    public static double frequencyDistribution(double[] arr, String num){
    	double count=0;
    	int number= Integer.parseInt(num);
    	Arrays.sort(arr);
    	int length=arr.length;
    	for(int i=0; i<length; i++){
    		if(arr[i]==number)
    			count++;
    	}
    	return count;
    }
    
    
	@Override
	public void display(Object message) {
		// TODO Auto-generated method stub
		ArrayList<String> replyFromServer = (ArrayList<String>)message;
		if (	((String)(replyFromServer.get(0))).equalsIgnoreCase("uploadFile")	)
		{
			 System.out.println("in displayyyyyyyyyyyyyyyyyyyyy");
			 System.out.println((String)(replyFromServer.get(1)));
			if ( ((String)(replyFromServer.get(1))).equalsIgnoreCase("fileUploaded") )
			{
				//(new Dialog ("Your report was added, thank you.")).setVisible(true);
				System.out.println("report was added");

			}
			else
				if ( ((String)(replyFromServer.get(1))).equalsIgnoreCase("fileNotUploaded") )
				{
					(new Dialog ("An Error occured while writing to the database.")).setVisible(true);
				}
			

		}
		
	}
}

